package com.zmzncs.lmtc.module.park.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmzncs.lmtc.common.pojo.bolian.QuoteField;
import com.zmzncs.lmtc.module.park.pojo.bolian_pull.BolianPull;
import com.zmzncs.lmtc.module.park.dao.BolianPullDao;
import com.zmzncs.lmtc.module.park.pojo.bolian_pull.UpdateNewField;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.park.service.BolianPullService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.module.park.service.ParkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 停车场-泊链-拉取 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-03-15
 */
@Service
public class BolianPullServiceImpl extends ServiceImpl<BolianPullDao, BolianPull> implements BolianPullService {

    @Autowired
    BolianPullDao bolianPullDao;
    @Autowired
    ParkInfoService parkInfoService;

    /**查询未处理的更新停车场信息*/
    @Override
    public List<BolianPull> getPendingBolianPark() {
        List<BolianPull> list = bolianPullDao.getPendingBolianPark();
        return list;
    }

    @Override
    public List<QuoteField> quoteFieldFromBolian(String updateField) {
        List<QuoteField> quoteFields = JSONObject.parseArray(updateField, QuoteField.class);
        if (quoteFields != null && quoteFields.size() > 0){
            for (QuoteField field : quoteFields) {
                if ("park_name".equals(field.getField())){
                    field.setFieldDesc("车场名称");
                }
                if ("park_address".equals(field.getField())){
                    field.setFieldDesc("车场地址");
                }
                if ("telephone".equals(field.getField())){
                    field.setFieldDesc("车场电话");
                }
                if ("longitude".equals(field.getField())){
                    field.setFieldDesc("经度");
                }
                if ("latitude".equals(field.getField())){
                    field.setFieldDesc("纬度");
                }
                if ("total_park_space".equals(field.getField())){
                    field.setFieldDesc("总车位数");
                }
                if ("empty_park_space".equals(field.getField())){
                    field.setFieldDesc("空车位数");
                }
                if ("price_desc".equals(field.getField())){
                    field.setFieldDesc("收费描述");
                }
                if ("remark".equals(field.getField())){
                    field.setFieldDesc("备注");
                }
                if ("bolian_park_union_id".equals(field.getField())){
                    field.setFieldDesc("bolian_park_union_id");
                }
                if ("bolian_park_id".equals(field.getField())){
                    field.setFieldDesc("bolian_park_id");
                }
            }
        }
        return quoteFields;
    }

    @Override
    @Transactional
    public void updateNewDataField(UpdateNewField field) {

        BolianPull bolianPull = new BolianPull();
        bolianPull.setBolianComId(field.getBolianComId());
        bolianPull.setStatus(field.getStatus());
        //引入
        if (field.getStatus() == 2){
            String quoteField = JSON.toJSON(field.getBolianPullList()).toString();
            bolianPull.setQuoteField(quoteField);

            //泊链车场数据更新到数据库
            parkInfoService.updateDateToParkInfo(field.getBolianComId(),field.getBolianPullList());
        }

        bolianPullDao.updateByBolianComId(bolianPull);

    }
}
